package cn.lyjuan.first.netty._4_netty_serialize.msg.reader.impl;

import cn.lyjuan.first.netty._4_netty_serialize.cst.MessageConst;
import cn.lyjuan.first.netty._4_netty_serialize.msg.MyMessage;
import cn.lyjuan.first.netty._4_netty_serialize.msg.reader.IReader;
import cn.lyjuan.first.netty._4_netty_serialize.msg.reader.IWriter;
import io.netty.buffer.ByteBuf;

public class HelloReaderImpl implements IReader, IWriter
{
    @Override
    public String head()
    {
        return MessageConst.HEAD_HELLO;
    }

    @Override
    public int dataLength()
    {
        return 5;
    }

    @Override
    public String read(ByteBuf buf)
    {
        byte[] b = new byte[dataLength()];

        buf.readBytes(b);

        return new String(b, MessageConst.CHARSET);
    }

    @Override
    public MyMessage write()
    {
        MyMessage msg = new MyMessage();

        msg.setHead(MessageConst.HEAD_HELLO);
        msg.setData("0102030405");
        msg.setFoot(MessageConst.FOOT);

        return msg;
    }
}
